<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="部门名称"></el-input>
                </el-form-item>
                <el-select v-model="filters.state" placeholder="请选择">
                    <el-option label="全部" value=""></el-option>
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="0"></el-option>
                </el-select>
                <el-form-item>
                    <el-button type="primary" v-on:click="getUsers">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>
        <!--
        列表
        sortable 排序
        -->
        <el-table :data="users" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" label="序号" width="150" sortable>
            </el-table-column>
            <el-table-column prop="sn" label="部门编号" sortable>
            </el-table-column>
            <el-table-column prop="name" label="部门名称"  sortable>
            </el-table-column>
            <el-table-column prop="state" label="部门状态" :formatter="formatSex"  sortable>
            </el-table-column>
            <el-table-column prop="manager.username" label="部门经理"  sortable>
            </el-table-column>
            <el-table-column prop="department.name" label="上级部门"  sortable>
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="total, sizes, prev, pager, next, jumper"
                           @current-change="handleCurrentChange"
                           @size-change="handleSizeChange"
                           :current-page="currentPage"
                           :page-size="pageSize"
                           :page-sizes="pageSizes"
                           :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增界面-->
        <el-dialog title="新增" v-model="addFormVisible" :close-on-click-modal="false">
            <el-form :model="addForm" :rules="addFormRules" ref="addForm" label-width="80px">
                <el-form-item label="部门编号" prop="name">
                    <el-input v-model="addForm.sn" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="部门状态" prop="state">
                    <el-radio-group v-model="addForm.state">
                        <el-radio class="radio" :label="1">启用</el-radio>
                        <el-radio class="radio" :label="0">禁用</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="上级部门" prop="state">
                    <el-select v-model="addForm.parentId" placeholder="请选择">
                        <el-option v-for="item in parentDepartment"
                                   :key="item.id"
                                   :label="item.name"
                                   :value="item.id"
                        ></el-option>

                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="addFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>
export default {
    data() {
        return {
            addFormVisible:false,//关闭模态框
            users: [],//列表数据
            listLoading: false,//加载框
            sels: [],//列表选中列
            pageSize: 5,//每页显示的条数
            pageSizes: [5,10,15],
            currentPage:1,//当前页,
            total: 0,//总条数
            filters:{ //高级查询
                name:'',
                state:''
            },
            addForm:{//添加/修改双向绑定
                sn:'',
                name:'',
                state:1,
                parentId:'',
            },
            //所有部门列表
            parentDepartment:[],
            //表单验证 空值验证
            addFormRules: {
                name: [
                    { required: true, message: '请输入部门名称', trigger: 'blur' },
                ],
                sn:[
                    { required: true, message: '请输入部门编号', trigger: 'blur' },
                ],
            }
        };
    },
    methods: {
        //添加框 清除所有数据
        handleAdd(){
            //清空表单验证
            if(this.$refs.addForm){
                this.$refs.addForm.resetFields();
            }
            //清空值
            for(var k in this.addForm){
                this.addForm[k] = "";
            }

            //打开模态框
            this.addFormVisible = true;
        },
        //添加,修改列表 确定按钮
        addSubmit(){
            this.$refs["addForm"].validate((valid) => {
                if (valid) {
                    let para=Object.assign({},this.addForm);
                    let parent = {id:para.parentId};
                    para.department = parent;
                    para.manager = null;
                    if(para.id){
                        this.$http.post("/department",para).then((res => {
                            if(res.data.success){
                                this.addFormVisible = false;
                                this.loadDepartment()
                            }else {
                                this.$message.error(res.data.msg);
                            }
                        }));
                    }else {
                        this.$http.put("/department",para).then((res => {
                            if(res.data.success){
                                this.addFormVisible=false;
                                this.loadDepartment();
                            }else{
                                this.$message.error(res.data.msg)
                            }
                        }));
                    }
                }
            });
            // let partm= this.addForm;
            // partm.department={"id":partm.parentId}
        },
        //删除
        handleDel(index,row){
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.delete("/department/"+row.id).then((res =>{
                    //console.debug("删除",res);
                    if(res.data.success){
                        this.$message({
                            type: 'success',
                            message: '删除成功!',
                        });
                        this.loadDepartment()
                    }else {
                        this.$message.error(res.data.msg)
                    }
                }));
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //批量删除 挑选数据
        selsChange(sels){
            this.sels = sels;
        },
        //批量删除 确认按钮
        batchRemove(){
            var ids = this.sels.map(item => item.id);
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.patch("/department/batchDel",ids).then((res =>{
                    console.debug("批量删除",res);
                    if(res.data.success){
                        this.$message({
                            type: 'success',
                            message: '删除成功!',
                        });
                        this.loadDepartment()
                    }else {
                        this.$message.error(res.data.msg)
                    }
                }));
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },
        //编辑
        handleEdit(index,row){
            this.addFormVisible=true;
            //回显
            for(var k in row){
                this.addForm[k] = row[k]
            }
            //判断 并回显上级部门
            if(this.addForm.department){
                this.addForm.parentId = this.addForm.department.id;
            }
            //console.debug("测试上级部门",this.addForm)
            //console.debug("测试par",this.parentDepartment);
        },
        //高级查询
        getUsers(){
            this.loadDepartment();
        },
        //每页条数发生改变时，触发该函数
        handleSizeChange(val){
            this.currentPage=1;
            this.pageSize=val;
            this.loadDepartment();
        },
        //当页码发生改变时，触发函数
        handleCurrentChange(val) {
            this.currentPage=val;
            this.loadDepartment();
        },
        //部门状态
        formatSex(row){
            return row.state==0?"禁用":"启用"
        },
        //加载列表
        loadDepartment(){
            this.listLoading=true;
            let partm={"currentPage":this.currentPage,
                       "pageSize":this.pageSize,
                       "name":this.filters.name,
                       "state":this.filters.state};
            this.$http.patch("/department",partm).then((res =>{
                console.debug("加载列表",res);
                this.users = res.data.list;
                this.total = res.data.total;
                this.listLoading=false;
            }));
        },

        //加载所有部门
        loadAllParentDepartment(){
            this.$http.patch("/department/findDepartment").then((res => {
                console.debug("加载所有部门",res);
                this.parentDepartment = res.data
            }));
        }
    },
    mounted() {
        this.loadDepartment();
        this.loadAllParentDepartment();
    }
}

</script>